package com.mumu.test.deepseek;

import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import static com.mumu.test.deepseek.Config.*;

/**
 * @Author: MuMu
 * @Date: 2025/2/6 9:50
 */
public class DeepSeekClient {

    public interface Lis{
        void error();
        void msg(String msg);
        void end();
    }

    public void ask(List<RequestMessageItem> requestMessageItemList, boolean  stream, Lis lis) {
        //  创建OkHttp客户端
        OkHttpClient client = new OkHttpClient();

        // 把用户提问添加到请求中
        String requestBody = new RequestBodyParameter("deepseek-chat", requestMessageItemList, stream).toString();
        RequestBody body = RequestBody.create(requestBody, MediaType.parse("application/json; charset=utf-8"));

        Request request = new Request.Builder()
                .url(BASE_URL)
                .addHeader("Authorization", "Bearer " + API_KEY)
                .post(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                lis.error();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    try {
                        String responseData = response.body().string();
                        lis.msg(responseData);
                        lis.end();
                    } catch (Exception e){
                        lis.error();
                    }
                } else {
                    lis.error();
                }
            }
        });
    }

    public String getMsg(String msg){
        JSONObject jsonObject = new JSONObject(msg);
        JSONArray choices = jsonObject.getJSONArray("choices");
        return choices.getJSONObject(0).getJSONObject("message").getString("content");
    }

}


/*
{"id":"f49b1a3a-50bd-44ff-ae42-4fbea8bb7b7d","object":"chat.completion","created":1738807423,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"您好！我是由中国的深度求索（DeepSeek）公司开发的智能助手DeepSeek-V3。如您有任何任何问题，我会尽我所能为您提供帮助。"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":37,"total_tokens":42,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":5},"system_fingerprint":"fp_3a5770e1b4"}
{"id":"7941eb07-5196-44af-8261-c51b310e5096","object":"chat.completion","created":1738807372,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"您好！我是由中国的深度求索（DeepSeek）公司开发的智能助手DeepSeek-V3。如您有任何任何问题，我会尽我所能为您提供帮助。"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":37,"total_tokens":42,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":5},"system_fingerprint":"fp_3a5770e1b4"}

 */